//
//  addCityViewController.m
//  天气预报
//
//  Created by 娟娟 on 15/9/20.
//  Copyright (c) 2015年 wang. All rights reserved.
//

#import "addCityViewController.h"
#import <sqlite3.h>
#import "mainViewController.h"
#import "MBProgressHUD.h"
#import "FMDatabase.h"
@interface addCityViewController ()<UITableViewDataSource,UITableViewDelegate>

@end

@implementation addCityViewController




-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
   // NSLog(@"ddditem is%d",item.tag);
    if (item.tag ==0) {
        /*int flag = 0;
        for (UIViewController *vcHome in self.navigationController.viewControllers) {
            if ([vcHome isKindOfClass:[mainViewController  class]]) {
                [self.navigationController popToViewController:vcHome animated:YES];
              
                flag = 1;
            }
        }
        
        if (flag == 1) {
         */
            mainViewController *mainVc = [[mainViewController alloc]init];
            [self.navigationController pushViewController:mainVc animated:YES];
       // }
    }
    if (item.tag == 1) {
        
        //如果超出五个,出现提示
        if (self.citys.count >=5) {
            MBProgressHUD *HUD = [[MBProgressHUD alloc]initWithView:self.view];
        
            [self.view addSubview:HUD];
            HUD.labelText = @"多于五个城市啦~~~请先删除~";
            HUD.mode = MBProgressHUDModeCustomView;
            [HUD showAnimated:YES whileExecutingBlock:^{
                sleep(2);
            } completionBlock:^{
                [HUD removeFromSuperview];
                //  HUD = nil;
            }];
        }
        else{
            //跳转到citylist
            cityListViewController *cityVC = [[cityListViewController alloc]initWithNibName:@"cityListViewController" bundle:nil];
            [self.navigationController pushViewController:cityVC animated:YES];
        
          }

    }
}

// -----------删除之后判断的代码就比较多呢
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [self.citys removeObjectAtIndex:indexPath.row];
        //删除了之后会直接挪到数组前面
        //界面上删除
        [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationBottom];
        NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString * docDirectory = paths[0];
        NSString *dateBaseSt = [docDirectory stringByAppendingPathComponent:@"date.sqlite"];
       
        FMDatabase *db = [FMDatabase databaseWithPath:dateBaseSt];
       
        if (![db open]) {
            NSLog(@"cannt open");
        }
        [db executeUpdate:@"drop table if exists CITYS;"];
        [db executeUpdate:@"create table  CITYS (ID integer  ,NAME text );"];
        if(self.citys.count != 0)
        {
            for (int i = 0; i<self.citys.count; i++) {
                // executeUpdate : 不确定的参数用?来占位
                //注意参数必须是对象形式的
                //[NSNumber numberWithInt:i],[self.citys[i]UTF8String ]];
                [db executeUpdate:@"INSERT INTO CITYS (ID, NAME) VALUES (?, ?);",
                 [NSNumber numberWithInt:i],self.citys[i]];
            }

            //[db executeUpdate:@"INSERT INTO CITYS (ID, NAME) VALUES (?, ?);", [NSNumber numberWithInt:@0],@"南京"];
        }
              [db close];
        

    }//insert好像显示不了啊，咋地
    else if (editingStyle == UITableViewCellEditingStyleInsert)
    {
        
    }
}




- (void)viewDidLoad {
    [super viewDidLoad];
    AppDelegate *appDelegate= [[UIApplication sharedApplication]delegate];
    self.citys = appDelegate.citys;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tabbar.delegate = self;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section

{
    return 50;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

{
    
    CGRect frameRect = CGRectMake(0, 0, 320, 50);
    CGRect frameRectlabel = CGRectMake(0, 0, 100, 45);
    CGRect frameRect2 = CGRectMake(0, 49, 320, 1);

    UIView *view = [[UIView alloc]initWithFrame:frameRect];
    UIView *view2 = [[UIView alloc]initWithFrame:frameRect2];
    view2.backgroundColor = [UIColor blueColor];
    UILabel *label = [[UILabel alloc] initWithFrame:frameRectlabel];
    label.text=@"城市管理";
 
    view.backgroundColor = [UIColor grayColor];
    
    [view addSubview:label];
    [view addSubview:view2];
    return view;
    
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return self.citys.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *ID = @"id";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
                             ID];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]
                initWithStyle:UITableViewCellStyleSubtitle
                reuseIdentifier:ID];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    }
    
    
    cell.textLabel.text = self.citys[indexPath.row];
     cell.imageView.image = [UIImage imageNamed:@"3.jpg"];
        return cell;
}







@end
